package com.cw.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cw.constant.Constants;
import com.cw.entity.OccupationBedLog;
import com.cw.mapper2.OccupationBedLogMapper;
import com.cw.service.IOccupationBedLogService;
import com.cw.service.ISysUserService;
import com.cw.util.HttpUtil;
import com.cw.util.UUIDUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;
import java.util.UUID;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author ly
 * @since 2021-06-10
 */
@Service
public class OccupationBedLogServiceImpl extends ServiceImpl<OccupationBedLogMapper, OccupationBedLog> implements IOccupationBedLogService{

    @Autowired
    private ISysUserService userService;

    /**
     * 标记删除日志
     * @param occupationId 占床业务主键
     * @param bedNo        床号
     * @return boolean
     * @author ly
     * @date 2021/6/10 11:33
     */
    @Override
    public boolean insertDeleteLog(String occupationId, String bedNo) {
        String userName = HttpUtil.getRequestHeaderInfo(Constants.USER_NAME);
        String userId = HttpUtil.getRequestHeaderInfo(Constants.USER_ID);
        //获取真实姓名
        String realName = userService.getById(userId).getRealname();

        OccupationBedLog occupationBedLog = new OccupationBedLog();
        occupationBedLog.setId(UUIDUtil.uuid());
        occupationBedLog.setOperCode(userName);
        occupationBedLog.setOperName(realName);
        occupationBedLog.setBedNo(bedNo);
        occupationBedLog.setOccupationBedId(occupationId);
        occupationBedLog.setCreateTime(LocalDateTime.now());
        occupationBedLog.setIp(getIpAddr());
        occupationBedLog.setOperation("更改为取消状态");
        //更新状态
        occupationBedLog.setState("2");
        return save(occupationBedLog);
    }

    /**
     * 插入日志
     * @param occupationId 占床业务主键
     * @param bedNo 床号
     * @param operNo 操作人工号
     * @param realName 真实姓名
     * @return boolean
     * @author ly
     * @date 2021/6/16 9:35
     */
    @Override
    public boolean insertLog(String occupationId, String bedNo, String operNo, String realName) {
        OccupationBedLog occupationBedLog = new OccupationBedLog();
        occupationBedLog.setId(UUIDUtil.uuid());
        occupationBedLog.setOperCode(operNo);
        occupationBedLog.setOperName(realName);
        occupationBedLog.setBedNo(bedNo);
        occupationBedLog.setOccupationBedId(occupationId);
        occupationBedLog.setCreateTime(LocalDateTime.now());
        occupationBedLog.setIp(getIpAddr());
        occupationBedLog.setState("0");
        occupationBedLog.setOperation("新建占床记录");
        return save(occupationBedLog);
    }

    /**
     * 获取IP地址
     */
    private String getIpAddr() {
        RequestAttributes ra = RequestContextHolder.getRequestAttributes();
        ServletRequestAttributes sra = (ServletRequestAttributes) ra;
        HttpServletRequest request = sra.getRequest();
        String ip = request.getHeader("x-forwarded-for");
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("Proxy-Client-IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("WL-Proxy-Client-IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getRemoteAddr();
        }
        return "0:0:0:0:0:0:0:1".equals(ip) ? "127.0.0.1" : ip;
    }
}
